#include <iostream>
#include "ServerSocket.h"
#include "SocketException.h"
#include <string.h>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
using namespace std;
int cube1 = 0;
int cube2 = 0;
int activePlayer = 1;
string dataForPlayer[2] = "";
string dataFromPlayer[2] = "";

void *playerSocket(void* n) 
{
	ServerSocket server(30001);
	while (1) {
		cout<<"1"<<endl;
		ServerSocket sock;
		server.accept (sock);
		cout<<"2"<<endl;
		try 
		{
			sock >> dataFromPlayer[(int)n - 30001];
			dataForPlayer[((int)n - 30000) % 2] = dataFromPlayer[(int)n - 30001];
			sock << dataForPlayer[(int)n - 30001];
			cout << "Data From "<<(int)n - 30001<<"for player" << activePlayer % 2 << "is: "<<dataFromPlayer[(int)n - 30001];
		} catch ( SocketException& e ) {
			cout<<"Oops!!!"<<endl;
		}	
	}
}

void getCubes()
{
	srand(time(NULL));
    	cube1 = 1 + rand()%6;
    	cube2 = 1 + rand()%6;
} 

int main ( int argc, int argv[] )
{
	std::cout << "running....\n";

	try
	{
		pthread_t tid[2];
		int port[2] = {30001, 30002};
		int player = 0;
		ServerSocket server ( 30000 );
		string data;
		string player1SendData;
		string player2SendData;
		while ( player <= 2)
		{
			ServerSocket sock;
			server.accept (sock);
			try
			{
				sock >> data;
				if(data.compare("pleaseAddMe") == 0) {
					player++;
					ostringstream ss;
     					ss << player;
					data = ss.str();
					sock << data;
					pthread_create(&tid[player - 1], NULL, playerSocket, (void*)port[player - 1]);
					continue;
				}
				if (data.compare("ActivePlayer1") == 0){
					activePlayer = 1;
					getCubes();
					continue;	
				} else if (data.compare("ActivePlayer2") == 0) {
					activePlayer = 2;
					getCubes();
					continue;
				}
				if (data.compare("GetActivePlayer") == 0) {
					data = static_cast<ostringstream*>( &(ostringstream() << activePlayer) )->str();
					cout << data << endl;
                                        sock << data;
					continue;
				}
				size_t found=data.find("Player1Send");
				if (found!=string::npos) {
					player1SendData = data.substr(found + 11);
					continue;
				}
				found=data.find("Player2Send");
				if (found!=string::npos) {
					player2SendData = data.substr(found + 11);
					continue;
				}
				if (data.compare("Player1Wait") == 0) {
					sock << player2SendData;
					continue;
				}
				if (data.compare("Player2Wait") == 0) {
					sock << player1SendData;
					continue;
				}
				if (data.compare("Cube1") == 0) { 
					data = static_cast<ostringstream*>( &(ostringstream() << cube1) )->str();
					sock << data;
					continue;
				}
				if (data.compare("Cube2") == 0) { 
					data = static_cast<ostringstream*>( &(ostringstream() << cube2) )->str();
					sock << data;
					continue;
				}
				if (data.compare("End") == 0) {
					if (activePlayer == 1) {
						activePlayer = 2;
					} else {
						activePlayer = 1;
					}
					getCubes();
				}
			}
			catch ( SocketException& ) {}
		}
	}
	catch ( SocketException& e )
	{
		std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
	}

	return 0;
}
